package com.vma.lottery.test;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.vma.lottery.base.Data;
import com.vma.lottery.base.JFinalModelCase;
import com.vma.lottery.base.RootClass;
import com.vma.lottery.model.LotteryRecord;
import com.vma.lottery.vo.LotteryRecordVo;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2017/3/27.
 */
public class InitData extends JFinalModelCase{
    LotteryRecord dao = LotteryRecord.dao;
    static HttpClient httpClient = new DefaultHttpClient();

    @Test
    public void getNext(){

    }

    @Test
    public void testDate() throws Exception{
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.setTime(format.parse("2017-03-25 21:35:14"));
        System.out.println(calendar.get(Calendar.MONTH)+1);
        System.out.println(calendar.get(Calendar.DATE));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1);
    }

    @Test
    public void testRebulid(){
        try {
//           Page<LotteryRecordVo> list = LotteryRecord.dao.qryLotteryRecordList();
//           System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static String doPost(int years) throws IOException {
        String url="http://c.apiplus.net/daily.do?token=241d71ea7d144963&code=hk6&date="+years+"-01-01&format=json";
        //POST的URL
        HttpPost httppost=new HttpPost(url);
        //建立HttpPost对象
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        //添加参数
        httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        //设置编码
        HttpResponse response=httpClient.execute(httppost);
        //发送Post,并返回一个HttpResponse对象
        if(response.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回
            String result= EntityUtils.toString(response.getEntity());
            System.out.print("============"+result+"============");
            return result;
        }
        return "";
    }
    @Test
    public void getRecord(){
        try {
            Gson gson = new Gson();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(int i = 1977;i<=2017;i++){
                String res = doPost(i);
                RootClass rootClass = gson.fromJson(res,RootClass.class);
                List<Data> datas = rootClass.getData();
                List<LotteryRecord> list = new ArrayList<LotteryRecord>();
                for (Data data:datas) {
                    String openCode = data.getOpencode();
                    String[] codeArr = openCode.split(",");
                    String[] left = codeArr[5].split("\\+");
                    LotteryRecord  record   =   new LotteryRecord().setCode1(Integer.valueOf(codeArr[0]))
                            .setCode2(Integer.valueOf(codeArr[1]))
                            .setCode3(Integer.valueOf(codeArr[2]))
                            .setCode4(Integer.valueOf(codeArr[3]))
                            .setCode5(Integer.valueOf(codeArr[4]))
                            .setCode6(Integer.valueOf(left[0]))
                            .setCode7(Integer.valueOf(left[1]))
                            .setExpect(Integer.valueOf(data.getExpect().substring(4,data.getExpect().length())))
                            .setYears(Integer.valueOf(data.getExpect().substring(0,4)))
                            .setOpencode(data.getOpencode())
                            .setOpentime(format.parse(data.getOpentime()))
                            .setOpentimestamp(String.valueOf(data.getOpentimestamp()));
                    list.add(record);
                }
                Db.batchSave(list,list.size());
                Thread.sleep(6000);
            }



        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
